package com.fan.shop.views.bean;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import com.fan.shop.businesslogic.TransactionReferenceGroupCreateProcess;
import com.fan.shop.businesslogic.TransactionReferenceGroupDeleteProcess;
import com.fan.shop.businesslogic.TransactionReferenceGroupUpdateProcess;
import com.fan.shop.global.JacksonMapper;
import com.fan.shop.services.TransactionReferenceGroupService;

@ManagedBean(name = "transactionReferenceGroupsBean")
@RequestScoped
public class TransactionReferenceGroupsBean {

	private static Logger logger = Logger
			.getLogger(TransactionReferenceGroupsBean.class);

	@ManagedProperty(value = "#{transactionReferenceGroupService}")
	private TransactionReferenceGroupService transactionReferenceGroupService;
	private String tableData;
	private int columnsSize;
	private List<String> columnNames;
	private String editGroupCode;
	private String editDescription;
	
	private String selectedValue;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TransactionReferenceGroupsBean.logger = logger;
	}

	public TransactionReferenceGroupService getTransactionReferenceGroupService() {
		return transactionReferenceGroupService;
	}

	public void setTransactionReferenceGroupService(
			TransactionReferenceGroupService transactionReferenceGroupService) {
		this.transactionReferenceGroupService = transactionReferenceGroupService;
	}

	public String getEditGroupCode() {
		return editGroupCode;
	}

	public void setEditGroupCode(String editGroupCode) {
		this.editGroupCode = editGroupCode;
	}

	public String getEditDescription() {
		return editDescription;
	}

	public void setEditDescription(String editDescription) {
		this.editDescription = editDescription;
	}

	public String getTableData() {
		ObjectMapper mapper = JacksonMapper.getInstance();

		Set<String> columnNames = new HashSet<String>();
		for (String name : transactionReferenceGroupService.getColumnNames()) {
			columnNames.add(name);

		}
		FilterProvider filters = new SimpleFilterProvider().addFilter(
				"myFilter",
				SimpleBeanPropertyFilter.filterOutAllExcept(columnNames));

		tableData = "\"aaData\": [";
		StringBuffer data = new StringBuffer();

		for (Object obj : transactionReferenceGroupService.getList()) {
			try {
				String line = mapper.writer(filters).writeValueAsString(obj);
				data = data.append(line);

			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String tmp = data.toString().replaceAll("\\{[^,:\\{\\}]*:", "[");
		tmp = tmp.replaceAll(",[^,:\\{\\}]*:", ",");

		tableData = tableData + tmp.replace("{", "[").replace("}", "],");
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}

		tableData = tableData + "]";

		return tableData;
	}

	public void setTableData(String tableData) {
		this.tableData = tableData;
	}

	public int getColumnsSize() {
		columnsSize = getColumnNames().size();
		return columnsSize;
	}

	public void setColumnsSize(int columnsSize) {
		this.columnsSize = columnsSize;
	}

	public List<String> getColumnNames() {
		columnNames = transactionReferenceGroupService.getColumnNames();
		return columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public String createTransactionReferenceGroupAction() {
		TransactionReferenceGroupCreateProcess transactionReferenceGroupCreateProcess = new TransactionReferenceGroupCreateProcess();
		transactionReferenceGroupCreateProcess.setName(editGroupCode);
		transactionReferenceGroupCreateProcess.setDescription(editDescription);
		transactionReferenceGroupCreateProcess.setTransactionReferenceGroupService(transactionReferenceGroupService);
		transactionReferenceGroupCreateProcess.process();
		
		return "tr_groups.faces";
	}

	public String updateTransactionReferenceGroupAction() {
		TransactionReferenceGroupUpdateProcess transactionReferenceGroupUpdateProcess = new TransactionReferenceGroupUpdateProcess();
		transactionReferenceGroupUpdateProcess.setName(editGroupCode);
		transactionReferenceGroupUpdateProcess.setDescription(editDescription);
		transactionReferenceGroupUpdateProcess.setTransactionReferenceGroupService(transactionReferenceGroupService);
		transactionReferenceGroupUpdateProcess.process();
		clearFields();
		return "tr_groups.faces";
	}

	public String deleteTransactionReferenceGroupAction() {
		
		TransactionReferenceGroupDeleteProcess transactionReferenceGroupDeleteProcess = new TransactionReferenceGroupDeleteProcess();
		transactionReferenceGroupDeleteProcess.setName(editGroupCode);
		transactionReferenceGroupDeleteProcess.setDescription(editDescription);
		transactionReferenceGroupDeleteProcess.setTransactionReferenceGroupService(getTransactionReferenceGroupService());
		int result = transactionReferenceGroupDeleteProcess.process();
		clearFields();
		return "tr_groups.faces";
	}
	
	public void clearFields()
	{
		editGroupCode = "";
		editDescription = "";
	}

}
